package com.intellij.licensecommon.client.impl;

import com.intellij.licensecommon.client.LicenseClientApplication;
import com.intellij.licensecommon.client.LicenseClientException;
import com.intellij.licensecommon.data.LicenseImpl;
import com.intellij.licensecommon.logging.Logger;
import com.intellij.licensecommon.logging.LoggerFactoryHolder;
import com.intellij.licensecommon.message.*;
import com.intellij.licensecommon.net.Address;
import com.intellij.licensecommon.net.Network;
import com.intellij.licensecommon.net.NetworkTarget;

import java.util.Timer;
import java.util.TimerTask;

public class ClientImpl implements NetworkTarget, MessageReceiver, MessageClient {
    private static final Logger LOG = LoggerFactoryHolder.LOGGER_FACTORY.getLogger("#com.intellij.licensecommon.client.impl.ClientImpl");
    private final Network network;
    private final ClientMessageFactory clientMessageFactory;
    private LicenseClientApplication licenseClientApplication;
    private volatile LicenseImpl license;
    private Timer timer;
    private static final long interval = getMinMs(10L);

    public ClientImpl(Network paramNetwork, ClientMessageFactory paramClientMessageFactory) {
        this.network = paramNetwork;
        this.clientMessageFactory = paramClientMessageFactory;
    }

    public void a(LicenseClientApplication clientApp) throws LicenseClientException, com.intellij.licensecommon.net.b {
        this.licenseClientApplication = clientApp;
        try {
            this.network.a(this);
        } catch (com.intellij.licensecommon.net.f localf) {
            throw new com.intellij.licensecommon.net.b("Network is already started.", localf);
        } catch (Throwable localThrowable) {
            g();
            throw new LicenseClientException(localThrowable);
        }
    }

    public void a(LicenseImpl license) {
        this.license = license;
        e();
        start();
    }

    private synchronized void start() {
        if (this.timer == null) {
            this.timer = new Timer("timer-int", true);
            this.timer.schedule(new TimerTask() {
                public void run() {
                    ClientImpl.this.e();
                }
            }, interval, interval);
        }
    }

    private synchronized void stop() {
        if (this.timer != null) {
            this.timer.cancel();
            this.timer = null;
        }
    }

    private static long getMinMs(long paramLong) {
        return paramLong * 60L * 1000L;
    }

    private void e() {
        a(this.clientMessageFactory.a(this.license));
    }

    private void a(Message message) {
        if (!this.network.isRunning()) {
            return;
        }
        try {
            this.network.a(message);
        } catch (Throwable localThrowable) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(localThrowable);
            }
        }
    }

    public void f() {
        LicenseImpl license = this.license;
        this.license = null;
        stop();
        a(this.clientMessageFactory.b(license));
    }

    public void g() {
        this.network.b();
    }

    public LicenseImpl getLicense() {
        return this.license;
    }

    public MessageFactory getMessageFactory() {
        return this.clientMessageFactory;
    }

    public void a(Address paramAddress) {
        stop();
        this.licenseClientApplication.a(paramAddress);
    }

    public void a(Message message, Address paramAddress) throws Exception {
        if (message.a(this)) {
            message.a(this, paramAddress);
        }
    }

    public void a(Throwable paramThrowable) {
        paramThrowable.printStackTrace();
    }

    public void b(Throwable paramThrowable) {
        paramThrowable.printStackTrace();
    }
}
